<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/konto.php";
$konto = new Konto();
if(isset($_REQUEST['id']) && $_REQUEST['id']!="") {
    $konto = new Konto($_REQUEST['id']);
}
if(isset($_REQUEST['save'])) {
    $konto->setName($_REQUEST['name']);
    $konto->setBlz($_REQUEST['blz']);
    $konto->setNumber($_REQUEST['nr']);
    $konto->setIban($_REQUEST['iban']);
    $konto->setBic($_REQUEST['bic']);
    $konto->save();
}
?>
<?php
if($action=='edit') {
    $action = 'add';
}
if($action=='add') {
    $form_display = "block";
} else {
    $form_display = "none";
}
?>
<form id='stammdaten_konto_add_form' method="get" action="/index.php" style='display:<?php echo $form_display; ?>'>
    <input type="hidden" name="page" value="<?php echo $menuid;?>" />
    <input type="hidden" name="id" value="<?php echo $konto->getId(); ?>" />
    <div>
        <div class='inline-block label'>Name:</div>
        <div class='inline-block'>
            <input type="text" name="name" maxlength="100" value="<?php echo $konto->getName(); ?>" /><br />
        </div>
    </div>
    <div>
        <div class='inline-block label'>BLZ:</div>
        <div class='inline-block'>
            <input type="blz" name="blz" maxlength="8" value="<?php echo $konto->getBlz(); ?>" /><br />
        </div>
    </div>
    <div>
        <div class='inline-block label'>Kontonummer:</div>
        <div class='inline-block'>
            <input type="nr" name="nr" maxlength="10" value="<?php echo $konto->getNumber(); ?>" /><br />
        </div>
    </div>
    <div>
        <div class='inline-block label'>IBAN:</div>
        <div class='inline-block'>
            <input type="iban" name="iban" maxlength="22" value="<?php echo $konto->getIban(); ?>" /><br />
        </div>
    </div>
    <div>
        <div class='inline-block label'>BIC:</div>
        <div class='inline-block'>
            <input type="bic" name="bic" maxlength="11" value="<?php echo $konto->getBic(); ?>" /><br />
        </div>
    </div>
    <div>
        <button type="submit" name="save">Speichern</button>
        <button type="submit" name="reset">Abbrechen</button>
    </div>
</form>
<?php
if($action!="add") {
    $konton = Konto::getAll();
?>
<div>
    <a href='?page=<?php echo $menuid;?>&action=add'>Neues Konto</a>
</div>
<div>
    <table border="1" bordercolor="black" cellpadding="0" cellspacing="0" >
        <tr>
            <th></th>
            <th>Name</th>
            <th>BLZ</th>
            <th>Kontonummer</th>
            <th>IBAN</th>
            <th>BIC</th>
            <th></th>
        </tr>
<?php
if($konten!=NULL) {
    for($i=0;$i<count($konten);$i++) {
        $value = $konten[$i];
        $css_class = "even";
        if($i%2==1) {
            $css_class = "odd";
        }
?>
        <tr class="<?php echo $css_class; ?>">
            <td><?php echo ($i+1); ?></td>
            <td><?php echo $value->getName();?></td>
            <td><?php echo $value->getBlz();?></td>
            <td><?php echo $value->getNumber();?></td>
            <td><?php echo $value->getIban();?></td>
            <td><?php echo $value->getBic();?></td>
            <td><a href="?page=<?php echo $menuid; ?>&action=edit&id=<?php echo $value->getId();?>">edit</a></td>
        </tr>
<?php
    }
}
?>
    </table>
</div>
<?php    
}
?>